package goiot.api;

import com.fasterxml.jackson.core.type.TypeReference;

import goiot.GoiotHttp;
import goiot.dto.JsonResp;
import goiot.dto.NetworkConfig;
import goiot.dto.Product;

/**
 * 产品相关API
 */
public class ProductApi extends CurdApi<Product> {


	public ProductApi() {
	}

	@Override
	protected String getFullPath(String path) {
		return "/api/product" + path;
	}
	
	/**
	 * 发布
	 * 
	 * @param productId
	 * @return
	 */
	public JsonResp<Void> deploy(String productId) {
		JsonResp<Void> resp = GoiotHttp.post(getFullPath("/" + productId + "/deploy"), VOID_RESP);
		return resp;
	}

	/**
	 * 取消发布
	 * 
	 * @param productId
	 * @return
	 */
	public JsonResp<Void> undeploy(String productId) {
		JsonResp<Void> resp = GoiotHttp.post(getFullPath("/" + productId + "/undeploy"), VOID_RESP);
		return resp;
	}

	/**
	 * 保存物模型
	 * 
	 * @param productId
	 * @return
	 */
	public JsonResp<Void> saveTSL(String productId, String metadata) {
		Product product = new Product();
		product.setMetadata(metadata);
		JsonResp<Void> resp = GoiotHttp.put(getFullPath("/" + productId + "/tsl"), product, VOID_RESP);
		return resp;
	}

	/**
	 * 保存编解码脚本
	 * 
	 * @param productId
	 * @return
	 */
	public JsonResp<Void> saveScript(String productId, String script) {
		Product product = new Product();
		product.setScript(script);
		JsonResp<Void> resp = GoiotHttp.put(getFullPath("/" + productId + "/script"), product, VOID_RESP);
		return resp;
	}

	/**
	 * 查询网络配置
	 * 
	 * @param productId
	 * @return
	 */
	public JsonResp<NetworkConfig> getNetwork(String productId) {
		JsonResp<NetworkConfig> resp = GoiotHttp.get(getFullPath("/network/" + productId),
				new TypeReference<JsonResp<NetworkConfig>>() {
				});
		return resp;
	}

	/**
	 * 修改网络配置
	 * 
	 * @param config
	 * @return
	 */
	public JsonResp<Void> updateNetwork(NetworkConfig config) {
		JsonResp<Void> resp = GoiotHttp.put(getFullPath("/network/"), config, VOID_RESP);
		return resp;
	}

	/**
	 * 启动网络
	 * 
	 * @param productId
	 * @return
	 */
	public JsonResp<Void> startNetwork(String productId) {
		JsonResp<Void> resp = GoiotHttp.post(getFullPath("/network/" + productId), VOID_RESP);
		return resp;
	}
}
